package com.prs.crm.dao.oa.impl;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.JobPlanDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.JobPlan;

public class JobPlanDaoImpl extends BaseDaoImpl<JobPlan> implements JobPlanDao{

	@Override
	@SuppressWarnings("unchecked")
	public Collection<JobPlan> getLastJobPlan(final String type, final Person person, final Date current) {
		// TODO Auto-generated method stub
		return (Collection<JobPlan>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(JobPlan.class);
				criteria.add(Restrictions.eq("type", type));
				criteria.add(Restrictions.eq("recorder", person));
				
				Calendar ca = Calendar.getInstance();
				ca.setTime(current);
				
				if(type.equals(Constrains.JOBPLAN_DAILYPLAN)) {
					ca.set(Calendar.DAY_OF_YEAR, -2);
					
					criteria.add(Restrictions.gt("startTime", ca.getTime()));
					criteria.add(Restrictions.lt("endTime", current));
				}else if(type.equals(Constrains.JOBPLAN_MONTHLYPLAN)) {
					ca.set(Calendar.MONTH, -1);
					
					criteria.add(Restrictions.le("startTime", ca.getTime()));
					criteria.add(Restrictions.ge("endTime", ca.getTime()));							
				}else if(type.equals(Constrains.JOBPLAN_WEEKLYPLAN)) {
					ca.set(Calendar.WEEK_OF_YEAR, -1);
					
					criteria.add(Restrictions.le("startTime", ca.getTime()));
					criteria.add(Restrictions.ge("endTime", ca.getTime()));					
				}
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<JobPlan> getJobPlanByAudit(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<JobPlan>)this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(JobPlan.class);
				criteria.add(Restrictions.eq("auditPerson", person));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<JobPlan> getJobPlanByDate(final Date startTime, final Date endTime,
			final String type,final Person person) {
		// TODO Auto-generated method stub
		return (Collection<JobPlan>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(JobPlan.class);
				criteria.add(Restrictions.eq("recorder", person));
				criteria.add(Restrictions.eq("type", type));
				criteria.add(Restrictions.eq("startTime", startTime));
				criteria.add(Restrictions.eq("endTime", endTime));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<JobPlan> getJobPlanByKpi(final Integer year, final Integer month,
			final String type, final Person person) {
		// TODO Auto-generated method stub
		return (Collection<JobPlan>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Calendar start = Calendar.getInstance();
				start.set(Calendar.YEAR, year);
				start.set(Calendar.MONTH,month);
				start.set(Calendar.DAY_OF_MONTH, 1);
				Calendar end = Calendar.getInstance();
				end.set(Calendar.YEAR,year);
				end.set(Calendar.MONTH, month + 1);
				end.set(Calendar.DAY_OF_MONTH, 1);
				
				Criteria criteria = arg0.createCriteria(JobPlan.class);
				criteria.add(Restrictions.eq("type", type));
				criteria.add(Restrictions.eq("recorder", person));
				criteria.add(Restrictions.ge("startTime", start.getTime()));
				criteria.add(Restrictions.lt("startTime", end));

				return criteria.list();
			}
			
		});
	}

	@Override
	public Collection<?> getReportJobPlanSource(final String state, final String type,
			final Date datestart, final Date dateend, final Collection<Person> persons) {
		// TODO Auto-generated method stub
		return (Collection<?>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(JobPlan.class);
				if(state != null)
					criteria.add(Restrictions.eq("state", state));
				if(type != null)
					criteria.add(Restrictions.eq("type", type));
				if(datestart != null)
					criteria.add(Restrictions.ge("recordTime", datestart));
				if(dateend != null)
					criteria.add(Restrictions.le("recordTime", dateend));
				if(persons != null)
					criteria.add(Restrictions.in("recorder", persons));
				ProjectionList projectionlist=Projections.projectionList();  
	        	projectionlist.add(Projections.groupProperty("recorder"));
	        	projectionlist.add(Projections.rowCount());
	        	criteria.setProjection(projectionlist);
	        	
				return criteria.list();
			}});
	}

	@Override
	public Collection<?> getDeptReportJobPlanSource(final Date datestart, final Date dateend) {
		// TODO Auto-generated method stub
		return (Collection<?>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(JobPlan.class);
				if(datestart != null)
					criteria.add(Restrictions.ge("recordTime", datestart));
				if(dateend != null)
					criteria.add(Restrictions.le("recordTime", dateend));
				
				criteria.createAlias("recorder", "re");
				ProjectionList projectionlist=Projections.projectionList();  
	        	projectionlist.add(Projections.groupProperty("re.dept"));
	        	projectionlist.add(Projections.rowCount());
	        	criteria.setProjection(projectionlist);
				return criteria.list();
			}});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<JobPlan> getValidCritiqueJobPlan(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<JobPlan>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(JobPlan.class);
				criteria.add(Restrictions.ne("state", Constrains.TASK_PASS));
				criteria.createAlias("copyToPersons", "c");
				criteria.add(Restrictions.eq("c.id", person.getId()));
				criteria.addOrder(Order.desc("recordTime"));
				return criteria.list();
			}
			
		});
	}

}
